﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.KCBG;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;

public partial class AddPM : System.Web.UI.Page
{
    /// <summary>
    /// 考察报告操作对象
    /// </summary>
    private KCBG_DAO kcbgDAO = new KCBG_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        
        String message = null;
        //获取分类编码
        String flbm = Request["flbm"];
        //年份
        String nf = Request["nf"];


        KCBG_FL kcbgFL = null;
        //验证年份
        if (null == nf || nf.Length != 4)
        {
            message = "错误：年份信息有误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        if (null == flbm || "".Equals(flbm))
        {
            message = "错误：分类编码为空";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        else
        {
            kcbgFL = kcbgDAO.GetKCBG_FLByFlbm(flbm);
            if (null == kcbgFL)
            {
                message = "错误：没有找到对应的分类编码";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return;
            }
        }

        //设置返回按钮链接
        this.backHyperLink.NavigateUrl = GlobalKCBG.LISTPM_PAGE + "?catalog=" + flbm;

        KCBG_PM kcbgPM = this.kcbgDAO.getMaxKCBG_PM(flbm, nf);
       
        
        //分类编码
        this.flbmTextBox.Text = kcbgFL.flbm;
        //分类名称
        this.flmcTextBox.Text = kcbgFL.flmc;
        //索引编码
        this.sybmTextBox.Text=flbm+'-'+nf;
        //年份
        this.nfTextBox.Text = nf;

        //报告编号
        this.bgbhTextBox.Text = "";
       
        //报告名称
        this.bgmcTextBox.Text ="";
        
        //报告单位
        this.bgdwTextBox.Text = "";
        //参加单位
        this.cjdwTextBox.Text = "";
        //国家地区
        this.gjdqTextBox.Text = "";
        //主要人员
        this.zyryTextBox.Text = "";
        //报告日期
        this.bgrqTextBox.Text = "";
        //出访日期
        this.cfrqTextBox.Text = "";
        //返回日期
        this.fhrqTextBox.Text = "";
        //专业领域
        this.zylyTextBox.Text = "";
        //主题词
        this.ztcTextBox.Text = "";
        //报告摘要
        this.cfrqTextBox.Text = "";
        //页数
        this.ysHiddenField.Value = "";
        //备注
        this.bzTextBox.Text = "";
        //开放
        this.kfCheckBox.Checked = false;
        //优秀
        this.yxCheckBox.Checked = false;
        //获取文件名信息,不包括后缀
        String wjm = null;
        if (null == kcbgPM)
        {
            wjm = GetWJM(null);
        }
        else
        {
            wjm = GetWJM(kcbgPM.wjm);
        }
        //文件名
        this.wjmTextBox.Text = wjm ;

        User user = (User)Session["login"];
        if (null == user)
        {
            GlobalMethod.RedirectLogin(Response);
        }
        else
        {
            this.dlrTextBox.Text = user.name;
        }
    }

    /// <summary>
    /// 获取用户填写的国研报告篇名信息
    /// </summary>
    /// <returns></returns>
    private KCBG_PM GetKCBGPM()
    {
        KCBG_PM kcbgPM = new KCBG_PM();
        //索引编码
        kcbgPM.sybm = this.sybmTextBox.Text;
        //分类编码
        kcbgPM.flbm = this.flbmTextBox.Text;
        //文件名
        kcbgPM.wjm = this.wjmTextBox.Text;
        //报告编号
        kcbgPM.bgbh = this.bgbhTextBox.Text.Trim();
        //报告名称
        kcbgPM.bgmc = this.bgmcTextBox.Text.Trim();
        //报告单位
        kcbgPM.bgdw = this.bgdwTextBox.Text.Trim();
        //参加单位
        kcbgPM.cjdw = this.cjdwTextBox.Text.Trim();
        //国家地区
        kcbgPM.gjdq = this.gjdqTextBox.Text.Trim();
        //主要人员
        kcbgPM.zyry= this.zyryTextBox.Text.Trim();
        //报告日期

        kcbgPM.bgrq = DateTime.Parse(this.bgrqTextBox.Text);
        //出访日期

        kcbgPM.cfrq = DateTime.Parse(this.cfrqTextBox.Text);

        //返回日期

        kcbgPM.fhrq = DateTime.Parse(this.fhrqTextBox.Text);
       
        //专业领域
        kcbgPM.zyly = this.zylyTextBox.Text.Trim();
        //主题词
        kcbgPM.ztc = this.ztcTextBox.Text.Trim();
        //报告摘要
        kcbgPM.bgzy =this.bgzyTextBox.Text.Trim();
        //页数
        kcbgPM.ys = Int32.Parse(this.ysHiddenField.Value);
        //备注
        kcbgPM.bz = this.bzTextBox.Text.Trim();
        //更新日期
        kcbgPM.gxrq = DateTime.Now;
        //登录人
        kcbgPM.dlr = this.dlrTextBox.Text;
        //开放
        kcbgPM.kf = this.kfCheckBox.Checked;
        //优秀
        kcbgPM.yx = this.yxCheckBox.Checked;

        return kcbgPM;

    }

    /// <summary>
    /// 获取下一个文件名，包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <returns></returns>
    private String GetWJM(String wjm)
    {
        String result = null;
        if (null == wjm || "".Equals(wjm))
        {
            result = this.sybmTextBox.Text + "-000.PDF";
        }
        else
        {
            //获取文件名的后三位流水号，并在其基础上加1
            int endIndex = wjm.LastIndexOf(".");

            String phString = wjm.Substring(endIndex - 3, 3);

            int ph = 0;

            try
            {
                ph = Int32.Parse(phString);
            }
            catch (Exception)
            {
            }

            //当前篇号+1
            ph += 1;

            phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 2)
            {
                phString = "0" + phString;
            }
            else if (phString.Length >3)
            {
                String message = "错误：篇名数量已经超过了1000篇";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return null;
            }

            result = this.sybmTextBox.Text + "-" + phString+".PDF";
        }
        return result;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateBGMC())
        {
            return;
        }
        if (!this.ValidateWJM())
        {
            return;
        }
        if (!this.ValidateBGRQ())
        {
            return;
        }
        if (!this.ValidateCFRQ())
        {
            return;
        }
        if (!this.ValidateFHRQ())
        {
            return;
        }
        
        if (!ValidateYWJMFileUpload())
        {
            return;
        }
        KCBG_PM kcbgPM = this.GetKCBGPM();
        if (OperationFile.Upload(this.ywjmFileUpload, kcbgPM.wjm))
        {
            if (this.kcbgDAO.InsertKCBG_PM(kcbgPM))
            {
                Response.Write("<Script>alert('添加考察报告篇名信息成功')</Script>");
                this.BindData();
            }
        }
        else
        {
            Response.Write("<Script>alert('添加考察报告篇名信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证文件名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateWJM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        KCBG_PM kcbgPM = this.kcbgDAO.GetKCBG_PMByWjm(this.wjmTextBox.Text);
        if (null != kcbgPM)
        {
            this.errorLabel.Text = "错误：该文件名已存在";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证报告日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBGRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.bgrqTextBox.Text = this.bgrqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.bgrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：报告日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证出访日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateCFRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.cfrqTextBox.Text = this.cfrqTextBox.Text.Trim();

        try
        {
            DateTime.Parse(this.cfrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：出访日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证返回日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateFHRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.fhrqTextBox.Text = this.fhrqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.fhrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：返回日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证报告名称
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBGMC()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if(null==this.bgmcTextBox .Text||"".Equals(this.bgmcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：报告名称不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证上传文件
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateYWJMFileUpload()
    {
        this.errorLabel.Text = "";
        Boolean flag = true;
        if (null == this.ywjmFileUpload || !this.ywjmFileUpload.HasFile)
        {
            this.errorLabel.Text = "错误：请选择上传的文件";
            flag = false;
        }
        else
        {
            if (!this.ywjmFileUpload.FileName.EndsWith(".PDF", true, null))
            {
                this.errorLabel.Text = "错误：请选择PDF类型文件";
                flag = false;
            }
            else
            {
                PdfReader pdf = new PdfReader(this.ywjmFileUpload.FileBytes);
                if (null != pdf)
                {
                    //文件大小不能超过10M
                    if (pdf.FileLength >= 10 * 1024 * 1024)
                    {
                        this.errorLabel.Text = "错误：选择的文件大小超过了10M";
                        pdf.Close();
                        return false;
                    }

                    this.ysHiddenField.Value = pdf.NumberOfPages.ToString();
                }

                pdf.Close();
            }
        }
        return flag;
    }

    protected void ztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.ztcTextBox.Text = GlobalMethod.Semicolon(this.ztcTextBox.Text);
    }
    protected void zyryTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zyryTextBox.Text = GlobalMethod.Semicolon(this.zyryTextBox.Text);
    }
    protected void bgdwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.bgdwTextBox.Text = GlobalMethod.Semicolon(this.bgdwTextBox.Text);
    }
    protected void cjdwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.cjdwTextBox.Text = GlobalMethod.Semicolon(this.cjdwTextBox.Text);
    }
}